/* Copyright (c) 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groupware;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;


@SuppressWarnings("serial")
public class Index extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse resp) 
  					throws IOException, ServletException {

    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();

    String authURL = (user != null) ? userService.createLogoutURL("/") : userService.createLoginURL("/");

    PersistenceManager pm = PMF.get().getPersistenceManager();
    /*
    Query query = pm.newQuery(Project.class, "owner == userParam");
    query.declareImports("import com.google.appengine.api.users.User");
    query.declareParameters("User userParam");

    List<Project> results = (List<Project>) query.execute(user);

    String[] errors = req.getParameterValues("error");
    if (errors == null) errors = new String[0];
     */
    String[] errors ={};
    List<String> results = new ArrayList<String>();

    req.setAttribute("req", req);
    req.setAttribute("errors", errors);
    req.setAttribute("files", results);
    req.setAttribute("authURL", authURL);
    req.setAttribute("user", user);
    RequestDispatcher dispatcher =
      req.getRequestDispatcher("WEB-INF/templates/projectForm.jsp");
    dispatcher.forward(req, resp);
  }
  public void doPost(HttpServletRequest req, HttpServletResponse resp) 
	throws IOException, ServletException {

		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		
		String authURL = (user != null) ? userService.createLogoutURL("/") : userService.createLoginURL("/");
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		String title = (String)req.getParameter("title");
		String projectId = req.getParameter("projectId");
		String parentProject = req.getParameter("parentProject");
		Date startDate = new Date();
		Boolean visibility = Boolean.parseBoolean(req.getParameter("visibility"));
		Date dueDate = new Date();
		try {
			startDate = (Date)
			(new SimpleDateFormat("dd-MM-yy")).parse(req.getParameter("startDate"));
			dueDate = (Date)
			(new SimpleDateFormat("dd-MM-yy")).parse(req.getParameter("dueDate"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String description = req.getParameter("description");

        Project project = new Project(user, projectId, title, description, startDate, visibility, dueDate);
        if(parentProject != null && parentProject != ""){
        	Key parentKey = KeyFactory.createKey("Project", Long.parseLong(parentProject));
        	System.out.println(parentKey.getId());
        	project.setParentProject(parentKey);
        }
        try {
            pm.makePersistent(project);
        } finally {
            pm.close();
        }
		
			//  Entity project = new Entity("String");
			 // project.setProperty("myNum",la);
			 // datastore.put(project);
		
		
		req.setAttribute("req", req);
		//req.setAttribute("errors", errors);
		//req.setAttribute("files", results);
		req.setAttribute("authURL", authURL);
		req.setAttribute("user", user);
		req.setAttribute("title", title+"ads");

		RequestDispatcher dispatcher =
		req.getRequestDispatcher("WEB-INF/templates/projectForm.jsp");
		dispatcher.forward(req, resp);
		}
}
